import { defineStore } from 'pinia';
import { get } from '@/utils/request';

export const useConfigStore = defineStore('config', {
  state: () => ({
    webConfig: {}
  }),
  actions: {
    setWebConfig(config) {
      this.webConfig = config;
    },
    async fetchConfig() {
      const res = await get('/setting/getAll');
      if (res.code === 200) {
        this.webConfig = res.data;
        document.title = res.data.site_name || '默认标题';
        return res.data;
      }
      return null;
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'webConfig',
        storage: localStorage,
      }
    ]
  }
});